/**
 * Initialize your data structure here.
 * @param {number} size
 */
export var MovingAverage = function (size) {
  this.size = size
  this.queue = []
}

/**
 * @param {number} val
 * @return {number}
 */
MovingAverage.prototype.next = function (val) {
  this.queue.push(val)
  let i = 0
  let sum = 0
  while (i < this.size && this.queue.length - i > 0) {
    sum += this.queue[this.queue.length - i - 1]
    i++
  }
  return sum / i
}

/**
 * Your MovingAverage object will be instantiated and called as such:
 * var obj = new MovingAverage(size)
 * var param_1 = obj.next(val)
 */
